<?php

namespace addons\shopro\controller\goods;

use addons\shopro\controller\Common;
use addons\shopro\service\Wallet as WalletService;
use app\admin\model\shopro\user\WalletLog;
use app\admin\model\user\Userfinancial;
use app\common\model\User;
use app\common\model\Userchildren;
use think\Db;

/**
 * 理财产品
 */
class Financial extends Common
{

    protected $noNeedLogin = ['index','detail'];
    protected $noNeedRight = ['*'];

    /**
     * @menu:理财列表
     * @Author:男人的坏
     * @Date:2024-07-18 11:14
     */
    public function index()
    {
        $model = new \app\admin\model\shopro\goods\Financial;
        $list = $model->where(['status'=>1])->order('id desc')->field('content,updatetime',true)->select();
        $this->success('获取成功',$list);
    }

    /**
     * @menu:理财详情
     * @Author:男人的坏
     * @Date:2024-07-18 11:14
     */

    public function detail()
    {
        $ids = input('ids');
        if(!$ids){
            $this->error('缺少参数');
        }
        $model = new \app\admin\model\shopro\goods\Financial;
        $info = $model->where(['id'=>$ids])->find();
        $this->success('获取成功', $info);
    }

    /**
     * @menu:购买理财
     * @Author:男人的坏
     * @Date:2024-07-18 11:15
     */
    public function buy_financial(){
        $model = new \app\admin\model\shopro\goods\Financial;
        $user   = auth_user();
        $ids = input('ids');
        if(!$ids){
            $this->error('缺少参数');
        }
        $info = $model->where(['id'=>$ids,'status'=>1])->find();
        if(!$info){
            $this->error('理财产品不存在');
        }
//        if($user->principal < $info->money){
//            $this->error('金积分不足');
//        }
        if($user->principal <=0){
            $this->error('金积分不足');
        }
       $is_Wallet = Db::transaction(function () use ($info,$user) {
             WalletService::change($user->id, 'principal', -$user->principal, 'buy_financial', [],
                '购买理财');
           $userFinancialModel = new Userfinancial();
           $userFinancialModel->user_id      = $user->id;
           $userFinancialModel->financial_id = $info->id;
           $userFinancialModel->all_day      = $info->day;
           $userFinancialModel->day_time     = strtotime(date('Y-m-d',time()+(86400*$info->day)));
           $userFinancialModel->principal    = $user->principal;
           $user_ids_arr = Userchildren::where(['children_id' => $user->id])->column('member_id');
            User::whereIn('id', $user_ids_arr)->where('is_qrcode', '=', 1)->setInc('principal_total_consume',$user->principal);
          return $userFinancialModel->save();
        });

        if($is_Wallet){
            $this->success('购买成功');
        }
        $this->error('购买失败');
    }

    /**
     * 我的理财产品
     */
    public function user_financial(){
        $userFinancialModel = new Userfinancial();
        $data = $userFinancialModel->with(['financial'])->where(['user_id'=>$this->auth->id])->order('id desc')->select();
        foreach ($data as $row) {
            $row->visible(['id','principal','claimed_day','createtime','status']);
            $row->visible(['financial']);
            $row->getRelation('financial')->visible(['title','years_rate']);
        }
        $this->success('ok',$data);
    }


    /**
     * @menu:理财明细
     */
    public function user_financial_log(){
        $model = new WalletLog();
        $ids = input('ids');
        if(!$ids){
            $this->error('缺少参数');
        }
        $data = $model->whereIn('event', 'interest,return_principal')->where(['user_id'=>$this->auth->id,'ext'=>$ids,'type'=>'score'])->paginate()->each(function ($query)use($ids){
            $userFinancialModel = new Userfinancial();
            $query->principal = $userFinancialModel->where(['id'=>$ids])->value('principal');
        });
        $this->success('ok',$data);
    }

}
